package com.graduation.rescuematerial.controller.applet;

import com.graduation.rescuematerial.service.ICatalogService;
import com.graduation.rescuematerial.service.UserService;
import com.graduation.rescuematerial.vo.QueryCatalogByCompanyNameVo;
import com.graduation.rescuematerial.vo.RespBean;
import com.graduation.rescuematerial.vo.RespBeanEnum;
import com.graduation.rescuematerial.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:CaiShuangLian
 * @FileName:
 * @Date:Created in  2022/4/24 16:31
 * @Version:
 * @Description:TODO
 */
@RestController
@RequestMapping("/applet/catalog")
@Api(tags = "Applet——物资种类模块")
public class AppletCatalogController {

    @Autowired
    private UserService userService;
    @Autowired
    private ICatalogService catalogService;

    @ApiOperation("物资模块获取物资种类")
    @ResponseBody
    @PostMapping("/all/catalog")
    public RespBean getCatalogByCompanyName(@RequestBody QueryCatalogByCompanyNameVo queryCatalogByCompanyNameVo, HttpServletRequest request, HttpServletResponse response){
        //获取cookie
//        String ticket= CookieUtil.getCookieValue(request,"userTicket");
        String ticket=queryCatalogByCompanyNameVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null ){
                String companyName=queryCatalogByCompanyNameVo.getWarehouseName();
                if(null==companyName || companyName.isEmpty()){
                    companyName=userVo.getCompanyName();
                }
                return catalogService.getCatalogByCompanyName(companyName);
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }
}
